//
//  LssVisitorView.swift
//  freeChatting
//
//  Created by mac on 16/4/17.
//  Copyright © 2016年 mac. All rights reserved.
//

import UIKit
import SnapKit
protocol LssVisitorViewDelegate: NSObjectProtocol{
    func loginClickAction()
}
class LssVisitorView: UIView {
   weak var VVDelegate: LssVisitorViewDelegate?
   override init(frame: CGRect) {
        super.init(frame: frame)
    backgroundColor = RGBColor(red: 237, green: 237, blue: 237)
    setUpSubviews()
    }
    //MARK:- 纯代码必须实现的方法
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    //MARK:- 添加子视图
    func setUpSubviews(){
        addSubview(circleImageView)
        //添加约束
        circleImageView.snp_makeConstraints { (make) -> Void in
            make.center.equalTo(self)
            
        }
//        setAniOfCircleImage()
        addSubview(coverImageView)
        //添加约束
        coverImageView.snp_makeConstraints { (make) -> Void in
            make.center.equalTo(self)
            
        }
        addSubview(houseImageView)
        houseImageView.snp_makeConstraints { (make) -> Void in
            make.center.equalTo(self)
            
        }
        addSubview(messageLabel)
        messageLabel.snp_makeConstraints { (make) -> Void in
            make.centerX.equalTo(self)
            make.top.equalTo(coverImageView.snp_bottom).offset(15)
            make.width.equalTo(240)
            make.height.equalTo(36)
    
        }
        addSubview(registerButton)
        registerButton.snp_makeConstraints { (make) -> Void in
            make.left.equalTo(messageLabel.snp_left)
            make.top.equalTo(messageLabel.snp_bottom).offset(15)
            make.width.equalTo(100)
            make.height.equalTo(36)
            
        }
        addSubview(loginButton)
        loginButton.snp_makeConstraints { (make) -> Void in
            make.right.equalTo(messageLabel.snp_right)
            make.top.equalTo(messageLabel.snp_bottom).offset(15)
            make.width.equalTo(100)
            make.height.equalTo(36)
            
        }
    }
    //MARK:- 可供子类调用的方法
    func setOwnData(imageName: String?, title: String?){
        if let img = imageName, let tit = title{
            houseImageView.image = UIImage(named: img)
            messageLabel.text = tit
            circleImageView.hidden = true
        }else{
        setAniOfCircleImage()
        }
        
    }
    //MARK:- 添加动画效果
    private func setAniOfCircleImage(){
    //创建动画
        let ani = CABasicAnimation(keyPath: "transform.rotation")
        //动画时长
        ani.duration = 15
        //toValue
        ani.toValue = 2 * M_PI
        //重复次数
        ani.repeatCount = MAXFLOAT
        //切换tabbar时动画不被移除
        ani.removedOnCompletion = false
        //添加动画行为者
        circleImageView.layer.addAnimation(ani, forKey: nil)
    }
    
    //MARK:- 点击进行注册和登录操作
    @objc private func loginClickAction(){
        VVDelegate?.loginClickAction()
    }
    //---------------------------懒加载控件--------------------------------//
    //MARK:- 懒加载圆圈
    
    private lazy var circleImageView:UIImageView = UIImageView(image: UIImage(named: "visitordiscover_feed_image_smallicon"))
    
    //MARK:- 懒加载蒙层
    
    private lazy var coverImageView:UIImageView = {
        let imageView = UIImageView()
        imageView.image = UIImage(named: "visitordiscover_feed_mask_smallicon")
        return imageView
        }()
    
    //MARK:- 懒加载中间图片
    
    private lazy var houseImageView: UIImageView = UIImageView(image: UIImage(named: "visitordiscover_feed_image_house"))
    
    //MARK:- 懒加载信息标签
    
    private lazy var messageLabel: UILabel = {
        let label = UILabel(textColor: UIColor.grayColor(), textFont: 14)
        
        //文字居中
        label.textAlignment = NSTextAlignment.Center
        //自动换行
        label.numberOfLines = 0
        //背景颜色
        label.backgroundColor = UIColor.clearColor()
        //文字
        label.text = "关注一些人，回这里看看有什么新鲜事关注一些人，回这里看看有什么新鲜事"
        
        return label
    }()
    
    //MARK:- 懒加载注册按钮
    
    private lazy var registerButton: UIButton = {
        let button = UIButton()
        button.backgroundColor = UIColor.whiteColor()
        button.setTitle("注册", forState: UIControlState.Normal)
        button.setTitleColor(themeColor, forState: UIControlState.Normal)
        //设置文字大小
        button.titleLabel?.font = UIFont.systemFontOfSize(14)
        //添加点击事件
        button.addTarget(self, action: "loginClickAction", forControlEvents: UIControlEvents.TouchUpInside)
        return button
        }()
    
    //MARK:- 懒加载登录按钮
    
    private lazy var loginButton: UIButton = {
        let button = UIButton()
        button.backgroundColor = UIColor.whiteColor()
        button.setTitle("登录", forState: UIControlState.Normal)
        button.setTitleColor(themeColor, forState: UIControlState.Normal)
        //设置文字大小
        button.titleLabel?.font = UIFont.systemFontOfSize(14)
        //添加点击事件
        button.addTarget(self, action: "loginClickAction", forControlEvents: UIControlEvents.TouchUpInside)
        return button
        }()


}
